<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />


<meta name="author" content="weiya" />


<title>Top 10 Data Mining Algorithms</title>

<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/cosmo.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/textmate.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<script src="mathjax.js"></script>

<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
  pre:not([class]) {
    background-color: white;
  }
</style>
<script type="text/javascript">
if (window.hljs) {
  hljs.configure({languages: []});
  hljs.initHighlightingOnLoad();
  if (document.readyState && document.readyState === "complete") {
    window.setTimeout(function() { hljs.initHighlighting(); }, 0);
  }
}
</script>



<style type="text/css">
h1 {
  font-size: 34px;
}
h1.title {
  font-size: 38px;
}
h2 {
  font-size: 30px;
}
h3 {
  font-size: 24px;
}
h4 {
  font-size: 18px;
}
h5 {
  font-size: 16px;
}
h6 {
  font-size: 12px;
}
.table th:not([align]) {
  text-align: left;
}
</style>

<link rel="stylesheet" href="style.css" type="text/css" />

</head>

<body>

<style type = "text/css">
.main-container {
  max-width: 940px;
  margin-left: auto;
  margin-right: auto;
}
code {
  color: inherit;
  background-color: rgba(0, 0, 0, 0.04);
}
img {
  max-width:100%;
  height: auto;
}
.tabbed-pane {
  padding-top: 12px;
}
.html-widget {
  margin-bottom: 20px;
}
button.code-folding-btn:focus {
  outline: none;
}
summary {
  display: list-item;
}
</style>


<style type="text/css">
/* padding for bootstrap navbar */
body {
  padding-top: 51px;
  padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar)  */
.section h1 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h2 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h3 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h4 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h5 {
  padding-top: 56px;
  margin-top: -56px;
}
.section h6 {
  padding-top: 56px;
  margin-top: -56px;
}
.dropdown-submenu {
  position: relative;
}
.dropdown-submenu>.dropdown-menu {
  top: 0;
  left: 100%;
  margin-top: -6px;
  margin-left: -1px;
  border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
  display: block;
}
.dropdown-submenu>a:after {
  display: block;
  content: " ";
  float: right;
  width: 0;
  height: 0;
  border-color: transparent;
  border-style: solid;
  border-width: 5px 0 5px 5px;
  border-left-color: #cccccc;
  margin-top: 5px;
  margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
  border-left-color: #ffffff;
}
.dropdown-submenu.pull-left {
  float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
  left: -100%;
  margin-left: 10px;
  border-radius: 6px 0 6px 6px;
}
</style>

<script>
// manage active state of menu based on current page
$(document).ready(function () {
  // active menu anchor
  href = window.location.pathname
  href = href.substr(href.lastIndexOf('/') + 1)
  if (href === "")
    href = "index.html";
  var menuAnchor = $('a[href="' + href + '"]');

  // mark it active
  menuAnchor.parent().addClass('active');

  // if it's got a parent navbar menu mark it active as well
  menuAnchor.closest('li.dropdown').addClass('active');
});
</script>

<div class="container-fluid main-container">

<!-- tabsets -->

<style type="text/css">
.tabset-dropdown > .nav-tabs {
  display: inline-table;
  max-height: 500px;
  min-height: 44px;
  overflow-y: auto;
  background: white;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.tabset-dropdown > .nav-tabs > li.active:before {
  content: "";
  font-family: 'Glyphicons Halflings';
  display: inline-block;
  padding: 10px;
  border-right: 1px solid #ddd;
}

.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
  content: "&#xe258;";
  border: none;
}

.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
  content: "";
  font-family: 'Glyphicons Halflings';
  display: inline-block;
  padding: 10px;
  border-right: 1px solid #ddd;
}

.tabset-dropdown > .nav-tabs > li.active {
  display: block;
}

.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
  border: none;
  display: inline-block;
  border-radius: 4px;
}

.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
  display: block;
  float: none;
}

.tabset-dropdown > .nav-tabs > li {
  display: none;
}
</style>

<script>
$(document).ready(function () {
  window.buildTabsets("TOC");
});

$(document).ready(function () {
  $('.tabset-dropdown > .nav-tabs > li').click(function () {
    $(this).parent().toggleClass('nav-tabs-open')
  });
});
</script>

<!-- code folding -->





<div class="navbar navbar-default  navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="index.html">Rmd Gallery</a>
    </div>
    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li>
  <a href="index.html">Home</a>
</li>
<li>
  <a href="https://esl.hohoweiya.xyz">ESL CN</a>
</li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        
      </ul>
    </div><!--/.nav-collapse -->
  </div><!--/.container -->
</div><!--/.navbar -->

<div class="fluid-row" id="header">



<h1 class="title toc-ignore">Top 10 Data Mining Algorithms</h1>
<h4 class="author"><em>weiya</em></h4>
<h4 class="date"><em>April 3, 2019</em></h4>

</div>


<p>本文参考 <a href="https://hackerbits.com/data/top-10-data-mining-algorithms-in-plain-r/">HackerBits’s Top 10 data mining algorithms in plain R</a>。</p>
<div id="c5.0--c4.5" class="section level2">
<h2>C5.0 (前身是 C4.5)</h2>
<p>算法细节详见 <a href="https://esl.hohoweiya.xyz/09-Additive-Models-Trees-and-Related-Methods/9.2-Tree-Based-Methods/index.html">9.2 基于树的方法(CART)</a>。</p>
<pre class="r"><code>library(C50)
library(printr)
# divide into traing data and test data
set.seed(1234)
train.idx = sample(1:nrow(iris), 100)
iris.train = iris[train.idx, ]
iris.test = iris[-train.idx, ]
# train C5.0
model = C5.0(Species ~ ., data = iris.train)
# predict
results = predict(object = model, newdata = iris.test, type = &quot;class&quot;)
# confusion matrix
table(results, iris.test$Species)</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">results/</th>
<th align="right">setosa</th>
<th align="right">versicolor</th>
<th align="right">virginica</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">setosa</td>
<td align="right">12</td>
<td align="right">0</td>
<td align="right">0</td>
</tr>
<tr class="even">
<td align="left">versicolor</td>
<td align="right">0</td>
<td align="right">21</td>
<td align="right">3</td>
</tr>
<tr class="odd">
<td align="left">virginica</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">14</td>
</tr>
</tbody>
</table>
</div>
<div id="k-means" class="section level2">
<h2>K-means</h2>
<p>算法细节详见 <a href="https://esl.hohoweiya.xyz/13-Prototype-Methods-and-Nearest-Neighbors/13.2-Prototype-Methods/index.html">13.2 原型方法</a>。</p>
<pre class="r"><code># run kmeans
model = kmeans(x = subset(iris, select = -Species), centers = 3)
# check results
table(model$cluster, iris$Species)</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">/</th>
<th align="right">setosa</th>
<th align="right">versicolor</th>
<th align="right">virginica</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">1</td>
<td align="right">0</td>
<td align="right">46</td>
<td align="right">50</td>
</tr>
<tr class="even">
<td align="left">2</td>
<td align="right">17</td>
<td align="right">4</td>
<td align="right">0</td>
</tr>
<tr class="odd">
<td align="left">3</td>
<td align="right">33</td>
<td align="right">0</td>
<td align="right">0</td>
</tr>
</tbody>
</table>
</div>
<div id="support-vector-machine" class="section level2">
<h2>Support Vector Machine</h2>
<p>算法细节详见 <a href="https://esl.hohoweiya.xyz/12-Support-Vector-Machines-and-Flexible-Discriminants/12.2-The-Support-Vector-Classifier/index.html">12.2 支持向量分类器</a>。</p>
<pre class="r"><code>library(e1071)
model = svm(Species ~ ., data = iris.train)
results = predict(object = model, newdata = iris.test, type = &quot;class&quot;)
table(results, iris.test$Species)</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">results/</th>
<th align="right">setosa</th>
<th align="right">versicolor</th>
<th align="right">virginica</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">setosa</td>
<td align="right">12</td>
<td align="right">0</td>
<td align="right">0</td>
</tr>
<tr class="even">
<td align="left">versicolor</td>
<td align="right">0</td>
<td align="right">20</td>
<td align="right">1</td>
</tr>
<tr class="odd">
<td align="left">virginica</td>
<td align="right">0</td>
<td align="right">1</td>
<td align="right">16</td>
</tr>
</tbody>
</table>
</div>
<div id="apriori" class="section level2">
<h2>Apriori</h2>
<p>算法细节详见 <a href="https://esl.hohoweiya.xyz/14-Unsupervised-Learning/14.2-Association-Rules/index.html">14.2 关联规则</a>。</p>
<pre class="r"><code>library(arules)
data(&quot;Adult&quot;)
rules = apriori(Adult,
                parameter = list(support = 0.4, confidence = 0.7),
                appearance = list(rhs = c(&quot;race=White&quot;, &quot;sex=Male&quot;), default = &quot;lhs&quot;))</code></pre>
<pre><code>## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.7    0.1    1 none FALSE            TRUE       5     0.4      1
##  maxlen target   ext
##      10  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 19536 
## 
## set item appearances ...[2 item(s)] done [0.00s].
## set transactions ...[115 item(s), 48842 transaction(s)] done [0.04s].
## sorting and recoding items ... [11 item(s)] done [0.01s].
## creating transaction tree ... done [0.02s].
## checking subsets of size 1 2 3 4 5 done [0.00s].
## writing ... [32 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].</code></pre>
<pre class="r"><code># results
rules.sorted = sort(rules, by = &quot;lift&quot;)
top5.rules = head(rules.sorted, 5)
as(top5.rules, &quot;data.frame&quot;)</code></pre>
<table>
<thead>
<tr class="header">
<th></th>
<th align="left">rules</th>
<th align="right">support</th>
<th align="right">confidence</th>
<th align="right">lift</th>
<th align="right">count</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>2</td>
<td align="left">{relationship=Husband} =&gt; {sex=Male}</td>
<td align="right">0.4036485</td>
<td align="right">0.9999493</td>
<td align="right">1.495851</td>
<td align="right">19715</td>
</tr>
<tr class="even">
<td>12</td>
<td align="left">{marital-status=Married-civ-spouse,relationship=Husband} =&gt; {sex=Male}</td>
<td align="right">0.4034028</td>
<td align="right">0.9999492</td>
<td align="right">1.495851</td>
<td align="right">19703</td>
</tr>
<tr class="odd">
<td>3</td>
<td align="left">{marital-status=Married-civ-spouse} =&gt; {sex=Male}</td>
<td align="right">0.4074157</td>
<td align="right">0.8891818</td>
<td align="right">1.330151</td>
<td align="right">19899</td>
</tr>
<tr class="even">
<td>4</td>
<td align="left">{marital-status=Married-civ-spouse} =&gt; {race=White}</td>
<td align="right">0.4105892</td>
<td align="right">0.8961080</td>
<td align="right">1.048027</td>
<td align="right">20054</td>
</tr>
<tr class="odd">
<td>19</td>
<td align="left">{workclass=Private,native-country=United-States} =&gt; {race=White}</td>
<td align="right">0.5433848</td>
<td align="right">0.8804113</td>
<td align="right">1.029669</td>
<td align="right">26540</td>
</tr>
</tbody>
</table>
</div>
<div id="em" class="section level2">
<h2>EM</h2>
<p>算法细节详见 <a href="https://esl.hohoweiya.xyz/08-Model-Inference-and-Averaging/8.5-The-EM-Algorithm/index.html">8.5 EM 算法</a>。</p>
<pre class="r"><code>library(mclust)
model = Mclust(subset(iris, select = -Species))</code></pre>
<pre><code>## fitting ...
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=                                                                |   1%
  |                                                                       
  |=                                                                |   2%
  |                                                                       
  |==                                                               |   2%
  |                                                                       
  |==                                                               |   3%
  |                                                                       
  |===                                                              |   4%
  |                                                                       
  |===                                                              |   5%
  |                                                                       
  |====                                                             |   6%
  |                                                                       
  |=====                                                            |   7%
  |                                                                       
  |=====                                                            |   8%
  |                                                                       
  |======                                                           |   9%
  |                                                                       
  |=======                                                          |  10%
  |                                                                       
  |=======                                                          |  11%
  |                                                                       
  |========                                                         |  12%
  |                                                                       
  |========                                                         |  13%
  |                                                                       
  |=========                                                        |  13%
  |                                                                       
  |=========                                                        |  14%
  |                                                                       
  |==========                                                       |  15%
  |                                                                       
  |==========                                                       |  16%
  |                                                                       
  |===========                                                      |  17%
  |                                                                       
  |============                                                     |  18%
  |                                                                       
  |============                                                     |  19%
  |                                                                       
  |=============                                                    |  20%
  |                                                                       
  |==============                                                   |  21%
  |                                                                       
  |==============                                                   |  22%
  |                                                                       
  |===============                                                  |  23%
  |                                                                       
  |===============                                                  |  24%
  |                                                                       
  |================                                                 |  24%
  |                                                                       
  |================                                                 |  25%
  |                                                                       
  |=================                                                |  26%
  |                                                                       
  |=================                                                |  27%
  |                                                                       
  |==================                                               |  28%
  |                                                                       
  |===================                                              |  29%
  |                                                                       
  |===================                                              |  30%
  |                                                                       
  |====================                                             |  31%
  |                                                                       
  |=====================                                            |  32%
  |                                                                       
  |=====================                                            |  33%
  |                                                                       
  |======================                                           |  34%
  |                                                                       
  |=======================                                          |  35%
  |                                                                       
  |========================                                         |  36%
  |                                                                       
  |========================                                         |  37%
  |                                                                       
  |=========================                                        |  38%
  |                                                                       
  |=========================                                        |  39%
  |                                                                       
  |==========================                                       |  39%
  |                                                                       
  |==========================                                       |  40%
  |                                                                       
  |===========================                                      |  41%
  |                                                                       
  |===========================                                      |  42%
  |                                                                       
  |============================                                     |  43%
  |                                                                       
  |=============================                                    |  44%
  |                                                                       
  |=============================                                    |  45%
  |                                                                       
  |==============================                                   |  46%
  |                                                                       
  |===============================                                  |  47%
  |                                                                       
  |===============================                                  |  48%
  |                                                                       
  |================================                                 |  49%
  |                                                                       
  |================================                                 |  50%
  |                                                                       
  |=================================                                |  50%
  |                                                                       
  |=================================                                |  51%
  |                                                                       
  |==================================                               |  52%
  |                                                                       
  |==================================                               |  53%
  |                                                                       
  |===================================                              |  54%
  |                                                                       
  |====================================                             |  55%
  |                                                                       
  |====================================                             |  56%
  |                                                                       
  |=====================================                            |  57%
  |                                                                       
  |======================================                           |  58%
  |                                                                       
  |======================================                           |  59%
  |                                                                       
  |=======================================                          |  60%
  |                                                                       
  |=======================================                          |  61%
  |                                                                       
  |========================================                         |  61%
  |                                                                       
  |========================================                         |  62%
  |                                                                       
  |=========================================                        |  63%
  |                                                                       
  |=========================================                        |  64%
  |                                                                       
  |==========================================                       |  65%
  |                                                                       
  |===========================================                      |  66%
  |                                                                       
  |============================================                     |  67%
  |                                                                       
  |============================================                     |  68%
  |                                                                       
  |=============================================                    |  69%
  |                                                                       
  |==============================================                   |  70%
  |                                                                       
  |==============================================                   |  71%
  |                                                                       
  |===============================================                  |  72%
  |                                                                       
  |================================================                 |  73%
  |                                                                       
  |================================================                 |  74%
  |                                                                       
  |=================================================                |  75%
  |                                                                       
  |=================================================                |  76%
  |                                                                       
  |==================================================               |  76%
  |                                                                       
  |==================================================               |  77%
  |                                                                       
  |===================================================              |  78%
  |                                                                       
  |===================================================              |  79%
  |                                                                       
  |====================================================             |  80%
  |                                                                       
  |=====================================================            |  81%
  |                                                                       
  |=====================================================            |  82%
  |                                                                       
  |======================================================           |  83%
  |                                                                       
  |=======================================================          |  84%
  |                                                                       
  |=======================================================          |  85%
  |                                                                       
  |========================================================         |  86%
  |                                                                       
  |========================================================         |  87%
  |                                                                       
  |=========================================================        |  87%
  |                                                                       
  |=========================================================        |  88%
  |                                                                       
  |==========================================================       |  89%
  |                                                                       
  |==========================================================       |  90%
  |                                                                       
  |===========================================================      |  91%
  |                                                                       
  |============================================================     |  92%
  |                                                                       
  |============================================================     |  93%
  |                                                                       
  |=============================================================    |  94%
  |                                                                       
  |==============================================================   |  95%
  |                                                                       
  |==============================================================   |  96%
  |                                                                       
  |===============================================================  |  97%
  |                                                                       
  |===============================================================  |  98%
  |                                                                       
  |================================================================ |  98%
  |                                                                       
  |================================================================ |  99%
  |                                                                       
  |=================================================================| 100%</code></pre>
<pre class="r"><code>table(model$classification, iris$Species)</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">/</th>
<th align="right">setosa</th>
<th align="right">versicolor</th>
<th align="right">virginica</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">1</td>
<td align="right">50</td>
<td align="right">0</td>
<td align="right">0</td>
</tr>
<tr class="even">
<td align="left">2</td>
<td align="right">0</td>
<td align="right">50</td>
<td align="right">50</td>
</tr>
</tbody>
</table>
</div>
<div id="pagerank" class="section level2">
<h2>Pagerank</h2>
<p>算法细节详见 <a href="https://esl.hohoweiya.xyz/14-Unsupervised-Learning/14.10-The-Google-PageRank-Algorithm/index.html">14.10 谷歌的 PageRank 算法</a></p>
<pre class="r"><code>library(igraph)
library(dplyr)
# generate a random directed graph
set.seed(111)
g = random.graph.game(n = 10, p.or.m = 1/4, directed = TRUE)
plot(g)</code></pre>
<p><img src="top10_files/figure-html/unnamed-chunk-6-1.png" width="672" /></p>
<pre class="r"><code># calculate the pagerank for each object
pr = page.rank(g)$vector
# view results
df = data.frame(Object = 1:10, PageRank = pr)
arrange(df, desc(PageRank))</code></pre>
<table>
<thead>
<tr class="header">
<th align="right">Object</th>
<th align="right">PageRank</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="right">1</td>
<td align="right">0.1536549</td>
</tr>
<tr class="even">
<td align="right">8</td>
<td align="right">0.1533286</td>
</tr>
<tr class="odd">
<td align="right">9</td>
<td align="right">0.1508308</td>
</tr>
<tr class="even">
<td align="right">7</td>
<td align="right">0.1474552</td>
</tr>
<tr class="odd">
<td align="right">10</td>
<td align="right">0.1326519</td>
</tr>
<tr class="even">
<td align="right">5</td>
<td align="right">0.1050039</td>
</tr>
<tr class="odd">
<td align="right">6</td>
<td align="right">0.0801646</td>
</tr>
<tr class="even">
<td align="right">4</td>
<td align="right">0.0365532</td>
</tr>
<tr class="odd">
<td align="right">3</td>
<td align="right">0.0253568</td>
</tr>
<tr class="even">
<td align="right">2</td>
<td align="right">0.0150000</td>
</tr>
</tbody>
</table>
</div>
<div id="adaboost" class="section level2">
<h2>AdaBoost</h2>
<p>算法细节详见 <a href="https://esl.hohoweiya.xyz/10-Boosting-and-Additive-Trees/10.1-Boosting-Methods/index.html">10.1 boosting 方法</a>。</p>
<pre class="r"><code>library(adabag)
model = boosting(Species ~ ., data = iris.train)
results = predict(object = model, newdata = iris.test, type = &quot;class&quot;)
results$confusion</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">Predicted Class/Observed Class</th>
<th align="right">setosa</th>
<th align="right">versicolor</th>
<th align="right">virginica</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">setosa</td>
<td align="right">12</td>
<td align="right">0</td>
<td align="right">0</td>
</tr>
<tr class="even">
<td align="left">versicolor</td>
<td align="right">0</td>
<td align="right">20</td>
<td align="right">1</td>
</tr>
<tr class="odd">
<td align="left">virginica</td>
<td align="right">0</td>
<td align="right">1</td>
<td align="right">16</td>
</tr>
</tbody>
</table>
</div>
<div id="knn" class="section level2">
<h2>kNN</h2>
<p>算法细节详见 <a href="https://esl.hohoweiya.xyz/13-Prototype-Methods-and-Nearest-Neighbors/13.3-k-Nearest-Neighbor-Classifiers/index.html">k 最近邻分类器</a>。</p>
<pre class="r"><code>library(class)
results = knn(train = subset(iris.train, select = -Species),
              test = subset(iris.test, select = -Species),
              cl = iris.train$Species)
table(results, iris.test$Species)</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">results/</th>
<th align="right">setosa</th>
<th align="right">versicolor</th>
<th align="right">virginica</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">setosa</td>
<td align="right">12</td>
<td align="right">0</td>
<td align="right">0</td>
</tr>
<tr class="even">
<td align="left">versicolor</td>
<td align="right">0</td>
<td align="right">20</td>
<td align="right">1</td>
</tr>
<tr class="odd">
<td align="left">virginica</td>
<td align="right">0</td>
<td align="right">1</td>
<td align="right">16</td>
</tr>
</tbody>
</table>
</div>
<div id="naive-bayes" class="section level2">
<h2>Naive Bayes</h2>
<p>算法细节详见 <a href="https://esl.hohoweiya.xyz/06-Kernel-Smoothing-Methods/6.6-Kernel-Density-Estimation-and-Classification/index.html">6.6 核密度估计和分类</a>。</p>
<pre class="r"><code>model = naiveBayes(x = subset(iris.train, select = -Species), 
                   y = iris.train$Species)
results = predict(object = model, newdata = iris.test, type = &quot;class&quot;)
table(results, iris.test$Species)</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">results/</th>
<th align="right">setosa</th>
<th align="right">versicolor</th>
<th align="right">virginica</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">setosa</td>
<td align="right">12</td>
<td align="right">0</td>
<td align="right">0</td>
</tr>
<tr class="even">
<td align="left">versicolor</td>
<td align="right">0</td>
<td align="right">21</td>
<td align="right">1</td>
</tr>
<tr class="odd">
<td align="left">virginica</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">16</td>
</tr>
</tbody>
</table>
</div>
<div id="cart" class="section level2">
<h2>CART</h2>
<p>算法细节详见 <a href="https://esl.hohoweiya.xyz/09-Additive-Models-Trees-and-Related-Methods/9.2-Tree-Based-Methods/index.html">9.2 基于树的方法(CART)</a>。</p>
<pre class="r"><code>library(rpart)
model = rpart(Species ~ ., data = iris.train)
results = predict(object = model, newdata = iris.test, type = &quot;class&quot;)
table(results, iris.test$Species)</code></pre>
<table>
<thead>
<tr class="header">
<th align="left">results/</th>
<th align="right">setosa</th>
<th align="right">versicolor</th>
<th align="right">virginica</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">setosa</td>
<td align="right">12</td>
<td align="right">0</td>
<td align="right">0</td>
</tr>
<tr class="even">
<td align="left">versicolor</td>
<td align="right">0</td>
<td align="right">21</td>
<td align="right">3</td>
</tr>
<tr class="odd">
<td align="left">virginica</td>
<td align="right">0</td>
<td align="right">0</td>
<td align="right">14</td>
</tr>
</tbody>
</table>
</div>

<p>Copyright &copy; 2016-2019 weiya</p>



</div>

<script>

// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
  $('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
  bootstrapStylePandocTables();
});


</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>

</body>
</html>
